
package com.loong.loong.hik.service.impl;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.loong.loong.common.core.util.R;
import com.loong.loong.hik.entity.HikCdcUser;
import com.loong.loong.hik.mapper.HikCdcUserMapper;
import com.loong.loong.hik.property.urls.face.HikFaceUrl;
import com.loong.loong.hik.service.HikCdcUserService;
import com.loong.loong.hik.service.HikCdcVService;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * 疾控中心节目
 *
 * @author jinyiang
 * @date 2023-04-24 10:08:16
 */
@Service
public class HikCdcUserServiceImpl extends ServiceImpl<HikCdcUserMapper, HikCdcUser> implements HikCdcUserService {

	@Resource
	private HikCdcVService hikCdcVService;

	@Override
	public R add(HikCdcUser hikCdcUser) {
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.insertUser(hikCdcUser);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}

		HikCdcUser hikCdcUser1 = new HikCdcUser();

		hikCdcUser1.setAccount(hikCdcUser.getAccount());

		long count = count(Wrappers.query(hikCdcUser1));

		if (count>0){
			return R.failed("已存在相同的账户："+hikCdcUser1.getAccount());
		}else{
			return R.ok(save(hikCdcUser));
		}
	}

	@Override
	public R del(Integer id) {
		HikCdcUser byId = getById(id);
		if (byId==null){
			R.failed("删除失败，根据id查询为空");
		}
		if (StringUtils.isEmpty(byId.getAccount())){
			R.failed("删除失败，account为空");
		}
		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.delUser(byId);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(removeById(id));
	}

	@Override
	public R update(HikCdcUser hikCdcUser) {

		//掉接口开关
		if (HikFaceUrl.SWITCH.equals("Y")){
			try {
				//API 调用的结果存储在FaceGroupResult对象中
				R r = hikCdcVService.updateUser(hikCdcUser);
				if (r==null||r.getCode()!=0){
					return R.failed("接口调用失败，原因："+r.getMsg());
				}
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return R.ok(updateById(hikCdcUser));
	}
}
